package com.wu.client.schedule;

import cn.hutool.core.util.ObjectUtil;
import com.wu.client.cache.featuredData.FeaturedAlbumCache;
import com.wu.client.cache.featuredData.FeaturedArtistCache;
import com.wu.client.cache.featuredData.FeaturedMvCache;
import com.wu.client.constant.ResTypeEnum;
import com.wu.client.dto.AlbumMongo;
import com.wu.client.dto.ArtistMongo;
import com.wu.client.dto.MvMongo;
import com.wu.mongo.FeaturedData;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author Cousin
 * @date 2025/3/20
 * @Description
 */
@Component
@RequiredArgsConstructor
public class FeaturedSchedule {
    private final MongoTemplate mongoTemplate;
    @PostConstruct
    public void onStartup() {
        // 启动时立即调用任务
        refreshFeaturedAlbumCacheData();
        refreshFeaturedArtistCacheData();
        refreshFeaturedMvCacheData();
    }
    // 每天凌晨 4 点执行
    @Scheduled(cron = "0 0 4 * * ?")
    public void refreshFeaturedAlbumCacheData(){
        Integer code = ResTypeEnum.ALBUM.getCode();
        Query query = new Query();
        query.addCriteria(Criteria.where("resType").is(code));
        // 排序：按 createTime 字段降序
        query.with(Sort.by(Sort.Order.desc("createTime")));
        FeaturedData<AlbumMongo> featuredData = mongoTemplate.findOne(query, FeaturedData.class);
        if(ObjectUtil.isNotNull(featuredData)){
            List<AlbumMongo> info = featuredData.getInfo();
            FeaturedAlbumCache.getInstance().setAlbumMongoList(info);
        }
    }
    // 每天凌晨 4 点3分执行
    @Scheduled(cron = "0 3 4 * * ?")
    public void refreshFeaturedArtistCacheData(){
        Integer code = ResTypeEnum.ARTIST.getCode();
        Query query = new Query();
        query.addCriteria(Criteria.where("resType").is(code));
        // 排序：按 createTime 字段降序
        query.with(Sort.by(Sort.Order.desc("createTime")));
        FeaturedData<ArtistMongo> featuredData = mongoTemplate.findOne(query, FeaturedData.class);
        if(ObjectUtil.isNotNull(featuredData)){
            List<ArtistMongo> info = featuredData.getInfo();
            FeaturedArtistCache.getInstance().setArtistMongoList(info);
        }
    }
    @Scheduled(cron = "0 2 4 * * ?")
    public void refreshFeaturedMvCacheData(){
        Integer code = ResTypeEnum.MV.getCode();
        Query query = new Query();
        query.addCriteria(Criteria.where("resType").is(code));
        // 排序：按 createTime 字段降序
        query.with(Sort.by(Sort.Order.desc("createTime")));
        FeaturedData<MvMongo> featuredData = mongoTemplate.findOne(query, FeaturedData.class);
        if(ObjectUtil.isNotNull(featuredData)){
            List<MvMongo> info = featuredData.getInfo();
            FeaturedMvCache.getInstance().setMvMongoList(info);
        }
    }
}
